package handler

import (
	"context"
	"eat-server/global"
	"eat-server/models"
	__ "eat-server/proto"
	"fmt"
)

type Server struct {
	__.UnimplementedEatServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) EatAdd(_ context.Context, in *__.EatAddRequest) (*__.EatAddResponse, error) {
	eat := models.Eats{
		Name:   in.Name,
		Num:    int32(in.Num),
		Pricce: float64(in.Price),
	}
	global.DB.Create(&eat)
	return &__.EatAddResponse{
		Id: eat.Id,
	}, nil
}
func (s *Server) EatUpdate(_ context.Context, in *__.EatUpdateRequest) (*__.EatUpdateResponse, error) {
	//查商品存不存在
	var find models.Eats
	global.DB.Where("id = ?", in.Id).Find(&find)
	if find.Id == 0 {
		return nil, fmt.Errorf("商品不存在 不允许修改")
	}
	eat := models.Eats{
		Id:  in.Id,
		Num: int32(in.Num),
	}
	global.DB.Updates(&eat)
	return &__.EatUpdateResponse{
		Id: eat.Id,
	}, nil
}
